package com.crm.service.impl.opportunityManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.opportunityManagerCustomMapper.OpportunityCollaboratorCustomMapper;
import com.crm.dao.mapper.customMapper.opportunityManagerCustomMapper.OpportunityCustomMapper;
import com.crm.dao.mapper.opportunityManager.OpportunityCollaboratorMapper;
import com.crm.dao.mapper.opportunityManager.OpportunityMapper;
import com.crm.model.entity.opportunityManager.Opportunity;
import com.crm.model.entity.opportunityManager.OpportunityCollaborator;
import com.crm.model.entity.opportunityManager.OpportunityCollaboratorExample;
import com.crm.model.entity.opportunityManager.OpportunityExample;
import com.crm.model.qo.opportunityManager.OpportunityQO;
import com.crm.model.vo.opportunityManager.OpportunityCollaboratorVO;
import com.crm.model.vo.opportunityManager.OpportunityVO;
import com.crm.service.opportunityManager.OpportunityCollaboratorDbService;
import com.crm.service.opportunityManager.OpportunityDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 协作人
 */

@Service
public class OpportunityCollaboratorDbServiceImpl implements OpportunityCollaboratorDbService {

    @Autowired
    private OpportunityCollaboratorMapper opportunityCollaboratorMapper;

    @Autowired
    private OpportunityCollaboratorCustomMapper opportunityCollaboratorCustomMapper;

    /**
     * 商机-商机详情-查询所有用户
     * @param accountId 用户id
     * @param bs 1 查询全部  2查询单个
     * @return
     */
    @Override
    public List<OpportunityCollaboratorVO> userList(String accountId,Integer bs) {
        return opportunityCollaboratorCustomMapper.userList(accountId,bs);
    }

    /**
     * 商机管理-新增协作人
     * @param opportunityCollaborator 商机协作人实体
     * @return
     */
    @Override
    public Boolean addOpportunityCollaborator(OpportunityCollaborator opportunityCollaborator) {
        Boolean result = false;
        int num = opportunityCollaboratorMapper.insertSelective(opportunityCollaborator);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 商机-商机详情-查询当前商机的协作人
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public List<OpportunityCollaborator> opportunityCollaboratorList(String opportunityId) {
        OpportunityCollaboratorExample opportunityCollaboratorExample = new OpportunityCollaboratorExample();
        OpportunityCollaboratorExample.Criteria criteria = opportunityCollaboratorExample.createCriteria();
        criteria.andOpportunityIdEqualTo(opportunityId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<OpportunityCollaborator> list = opportunityCollaboratorMapper.selectByExample(opportunityCollaboratorExample);
        return list;
    }

    /**
     * 修改协作人
     * @param opportunityCollaborator 协作人实体
     * @return
     */
    @Override
    public Boolean updateOpportunityCollaborator(OpportunityCollaborator opportunityCollaborator) {
        Boolean result = false;
        int num = opportunityCollaboratorMapper.updateByPrimaryKeySelective(opportunityCollaborator);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 通过用户id和商机id查询是否这个协作人之前是废止的吗
     * @param opp 用户id和商机id
     * @return
     */
    @Override
    public OpportunityCollaborator queryIdAndOpportunityId(OpportunityCollaborator opp) {
        OpportunityCollaboratorExample opportunityCollaboratorExample = new OpportunityCollaboratorExample();
        OpportunityCollaboratorExample.Criteria criteria = opportunityCollaboratorExample.createCriteria();
        criteria.andAccountIdEqualTo(opp.getAccountId());
        criteria.andOpportunityIdEqualTo(opp.getOpportunityId());
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        List<OpportunityCollaborator> list = opportunityCollaboratorMapper.selectByExample(opportunityCollaboratorExample);
        if(list.size() !=0){
            return list.get(0);
        }else{
            return new OpportunityCollaborator();
        }

    }
}
